package com.itheima.travel.web;

import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.utils.ExceptionsUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AffixController.java
 * @Description 附件模块
 */
@RestController
@RequestMapping("/affix")
@Log4j2
@CrossOrigin
public class AffixController {

    @Autowired
    private AffixService affixService;

    /***
     * @description 文件上传
     * @param file 上传对象
     * @return: com.itheima.travel.req.AffixVo
     */
    @PostMapping(value = "upload/{businessType}")
    public ResponseWrap<AffixVo> upLoad(
            MultipartFile file,
            @PathVariable("businessType") String businessType)
            throws ProjectException {
        // 封装请求信息
        AffixVo affixVo = AffixVo.builder().businessType(businessType).build();
        AffixVo affixVoResult = null;
        try {
            affixVoResult = affixService.upLoad(file, affixVo);
            return ResponseWrap.<AffixVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(affixVoResult)
                    .build();
        } catch (IOException e) {
            log.error("文件上传异常：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.UPLOAD_FAIL.getCode(),
                    StatusEnum.UPLOAD_FAIL.getMsg());
        }
    }

    /***
     * @description 绑定业务
     * @param affixVo 附件信息
     * @return: java.lang.Boolean
     */
    @PostMapping("bindBusinessId")
    public ResponseWrap<Boolean> bindBusinessId(@RequestBody AffixVo affixVo) throws ProjectException {
        try {
            Boolean flag = affixService.bindBusinessId(affixVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("附件绑定业务异常：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.BIND_BUSINESS_FAIL.getCode(),
                    StatusEnum.BIND_BUSINESS_FAIL.getMsg());
        }

    }

    /**
     * @description 按业务ID查询附件
     * @param  businessId 业务ID
     * @return java.util.List<com.itheima.travel.req.AffixVo>
     */
    @GetMapping("{businessId}")
    public ResponseWrap<List<AffixVo>> findAffixByBusinessId(@PathVariable("businessId") Long businessId) throws ProjectException {
        try {
            AffixVo affixVo = AffixVo.builder().businessId(businessId).build();
            List<AffixVo> AffixVoList = affixService.findAffixByBusinessId(affixVo);
            return ResponseWrap.<List<AffixVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(AffixVoList)
                    .build();
        }catch (Exception e){
            log.error("查询业务对应附件：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_AFFIX_BUSINESSID_FAIL.getCode(),
                    StatusEnum.FIND_AFFIX_BUSINESSID_FAIL.getMsg());
        }
    }

}
